/**
 * 
 */
package testy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CCII_INTERFACE;
import cii_lib.JSserver;
import cii_lib.JSserverException;

public class TestHardware implements CCII_INTERFACE {
	
	private static final Logger log = LoggerFactory
			.getLogger(TestHardware.class);

	private boolean connected = true;

	public static long increment = 0;
	/**
	 * -1 : zero value<br>
	 * 0 : constant value of 51<br>
	 * 1 : random value between 0 - 250000<br>
	 * 2 : incrementation with random jump (250000)
	 */ 
	public static int incrementType = 0;

	public TestHardware() {
		log.debug("hard initiated " + (connected ? "REAL" : "FAKE"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cii_lib.CCII_INTERFACE#HardwareRead(long)
	 */
	public long HardwareRead(long addr) {
		// TODO Auto-generated method stub
		if (incrementType == 0) increment = 51;
		if (incrementType == 1)	increment = (long) (Math.random() * 250000);
		if (incrementType == 2)
			increment = increment + (long) (Math.random() * 250000);
		if (incrementType == -1) increment = 0;
		if (incrementType == 4 ) increment++;
		long tmp = increment;
		if (connected) {
			JSserver serv = new JSserver(4);
			try {
				serv.Open("192.168.228.1", 54321);
				tmp = serv.Read(addr);
				serv.Close();
			} catch (JSserverException e) {
				log.debug(e.getMessage());
			}
		}
		log.info("Reading hardware address:{} result:{} type{}", new String[]{Long.toString(addr), Long.toString(tmp), Integer.toString(incrementType)});
		return tmp;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cii_lib.CCII_INTERFACE#HardwareWrite(long, long)
	 */
	public void HardwareWrite(long addr, long data) {
		log.info("Writing hardware address:{} data:{}", new String[]{Long.toString(addr), Long.toString(data)});
		if (connected) {
			JSserver serv = new JSserver(4);
			try {
				serv.Open("192.168.228.1", 54321);
				serv.Write(addr, data);
				serv.Close();
			} catch (JSserverException e) {
				// TODO Auto-generated catch block
				log.error("", e);
			}
		}
		increment = data;
	}
}

/**
 * @author pdrabik
 * 
 */
